package com.qc31.monitorlib.ui.carsmonitor

import android.annotation.SuppressLint
import android.text.Html
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gyf.immersionbar.ktx.immersionBar
import com.qc31.baselib.databinding.IncludeToolbarBinding
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.baselib.utils.loadGif
import com.qc31.baselib.utils.loadStringImgError
import com.qc31.baselib.utils.stateColor
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.CarAround
import com.qc31.monitorlib.R
import com.qc31.monitorlib.base.BaseMapActivity
import com.qc31.monitorlib.databinding.ActivityAroundCarsBinding
import com.qc31.monitorlib.uitls.MapUtil
import com.qc31.monitorlib.uitls.MarkerBitmapUtil

/** @Author pc_hfjs @Date 2022/3/9 17:47 @Desc */
class CarAroundActivity :
    BaseMapActivity<ActivityAroundCarsBinding>(ActivityAroundCarsBinding::inflate) {
    private val mViewModel by viewModels<CarsAroundViewModel>()
    private val mAdapter by lazy { CarAroundAdapter() }
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.recyclerPoi,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData)
            .loadGif(this, R.mipmap.ic_empty)
        view
    }

    override fun initView() {
        initToolbar()
        binding.recyclerPoi.layoutManager = LinearLayoutManager(this)
        binding.recyclerPoi.adapter = mAdapter
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
        mViewModel.carId = carId
        mViewModel.getCarAround("3000")
        initRadioGroup()
    }


    override fun initMap() {
        MapUtil.initMapSetting(mAMap, true)

        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.aroundLive.observe(this) {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
            } else {
                addMarkers(it)
            }
            mAdapter.setList(it)
        }
    }

    override fun setListener() {
        binding.poiRdg.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.poiRdgOne -> mViewModel.getCarAround("500")
                R.id.poiRdgTwo -> mViewModel.getCarAround("1000")
                R.id.poiRdgThree -> mViewModel.getCarAround("1500")
                R.id.poiRdgFour -> mViewModel.getCarAround("2000")
                R.id.poiRdgFive -> mViewModel.getCarAround("2500")
                R.id.poiRdgSix -> mViewModel.getCarAround("3000")
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun initRadioGroup() {
        val unit = getString(R.string.unit_rice);
        binding.poiRdgOne.text = "500$unit"
        binding.poiRdgTwo.text = "1000$unit"
        binding.poiRdgThree.text = "1500$unit"
        binding.poiRdgFour.text = "2000$unit"
        binding.poiRdgFive.text = "2500$unit"
        binding.poiRdgSix.text = "3000$unit"
    }

    private fun initToolbar() {
        val plate = intent.getStringExtra(Keys.INTENT_CAR_NAME) ?: ""
        val mToolBarVb = IncludeToolbarBinding.bind(binding.root)
        immersionBar { titleBar(mToolBarVb.mToolbar) }
        ToolbarHelper.setToolbar(this, "$plate${getString(R.string.desc_cars_around)}")
    }


    private fun addMarkers(data: MutableList<CarAround>) {
        mAMap.clear()
        val builder = LatLngBounds.builder()
        data.forEach {
            val latLng = LatLng(it.glat.toDouble(), it.glng.toDouble())
            builder.include(latLng)
            MapUtil.addAndGetMarker(
                mAMap,
                latLng,
                MarkerBitmapUtil.getBitmapDes(
                    this,
                    mViewModel.carId,
                    it.state,
                    it.drct,
                    it.carName,
                    it.carPlate,
                    it.iconLink,
                    true,
                    it.speed
                )
            )
        }
        val build = builder.build()
        MapUtil.showMapBounds(mAMap, build, 240)
    }

    inner class CarAroundAdapter :
        BaseQuickAdapter<CarAround, BaseViewHolder>(R.layout.item_car_info_card) {
        override fun convert(holder: BaseViewHolder, item: CarAround) {
            holder.setText(R.id.tvPlate, item.carPlate)
                .setText(R.id.tvSpeed, "${item.speed}km/h")
                .setText(R.id.tvRunStop, item.runStopTime)

            val lrTvState = holder.getView<TextView>(R.id.tvStateAcc)
            val string =
                "<font color=${stateColor(item.state)} >${item.stateCn}</font><font color='#666666'> | ACC${item.accTime}</font>"
            lrTvState.text = Html.fromHtml(string)
            holder.getView<ImageView>(R.id.ivCarIcon).loadStringImgError(context, item.iconLink)
        }
    }
}